package org.jeecg.modules.demo.signatureCA.controller;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.demo.signatureCA.entity.TTGcasign;
import org.jeecg.modules.demo.signatureCA.service.ISignatureCAService;
import org.jeecg.modules.demo.signatureCA.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags="signatureCA")
@RestController
@RequestMapping("/demo/signatureCA")
@Slf4j
public class SignatureCAController {

    @Autowired
    private ISignatureCAService signatureCAService;


    /**
     * 获取当前登录人的要盖什么类型的章
     */
    @PostMapping(value = "/getQzlxForCA.jspx")
    @ResponseBody
//    public String getQzlxForCA(HttpServletRequest request,
//                        @RequestParam("hetid") String hetid,
//                        @RequestParam("userid") String userid,
//                        @RequestParam("hettype") String hettype) {
    public String getQzlxForCA(HttpServletRequest request,@RequestBody()String json){
        System.out.println("json: "+json);
        
        JsonParser jsonParser = new JsonParser();
        JsonElement jsonElement = jsonParser.parse(json);
        JsonObject jsonObject = jsonElement.getAsJsonObject();

        String hetid = jsonObject.get("hetid").getAsString();
        String userid = jsonObject.get("userid").getAsString();
        String hettype = jsonObject.get("hettype").getAsString();


        Map<String, Object> ht = new HashMap();
        Map ret = new HashMap();
        String objtag = "";
        if (hettype != null && hettype.equals("GL")) {
            objtag = "GL_HET";
            ht = signatureCAService.getGlHeInfoById(hetid);
        }
        else if (hettype != null && hettype.equals("SL")) {
            objtag = "SL_HET";
            ht = signatureCAService.getHeSLInfoById(hetid);
        }
        else {
            objtag = "TL_HET";
            ht = signatureCAService.getHeInfoById(hetid);
        }
        Map<String,Object> user = signatureCAService.getUserAndTraderByUserid(userid);
        String userId = user.get("USERID").toString();
        String qiyid = user.get("TRADERID").toString();
        if (null != ht && !ht.isEmpty()) {
            System.out.println(ht);
            String chumrssdwbm = ht.get("CHUMDWBM").toString();
            String maisrssdwbm = ht.get("MAISDWBM").toString();
            String chumdwbm = ht.get("CHUMDWBM").toString();
            Map<String,Object> qyxx = signatureCAService.getTraderInfoById(qiyid);
            if (qiyid.equals(maisrssdwbm)) {
                ret.put("result", "1");
            }
            else {
                ret.put("result", "0");
            }
            // else {
            // if (qyxx != null) {
            // String isqianz = qyxx.get("isqianz") == null ? "0" : qyxx
            // .get("isqianz").toString();
            // if (isqianz.equals("0")) {
            // ret.put("result", "7");
            // return JsonUtil.mapToJson(ret);
            // }
            // ret.put("result", "3");
            // }
            // else {
            // ret.put("result", "0");
            // }
            // }

        }
        else {
            ret.put("result", "9");
        }

        return JsonUtil.mapToJson(ret);
    }



    // CA盖章
    @PostMapping(value = "/saveHtqzCA.jspx")
    @ResponseBody
//    public String saveHtqzCA(HttpServletRequest request, String hetid, String userid,
//                      String hettype, String miwen, TTGcasign ttgcasign) {
    public String saveHtqzCA(HttpServletRequest request,@RequestBody()String json,TTGcasign ttgcasign){
        System.out.println("json: "+json);

        JsonParser jsonParser = new JsonParser();
        JsonElement jsonElement = jsonParser.parse(json);
        JsonObject jsonObject = jsonElement.getAsJsonObject();

        String hetid = jsonObject.get("hetid").getAsString();
        String userid = jsonObject.get("userid").getAsString();
        String hettype = jsonObject.get("hettype").getAsString();
        String miwen = jsonObject.get("miwen").getAsString();





        Map<String, Object> ht = new HashMap();
        Map ret = new HashMap();
        String signclob = request.getParameter("miwen");
        String objtag = "";
        if (hettype != null && hettype.equals("GL")) {
            objtag = "GL_HET";
            ht = signatureCAService.getGlHeInfoById(hetid);
        }
        else if (hettype != null && hettype.equals("SL")) {
            objtag = "SL_HET";
            ht = signatureCAService.getHeSLInfoById(hetid);
        }
        else {
            objtag = "TL_HET";
            ht = signatureCAService.getHeInfoById(hetid);
        }
        Map<String,Object> user = signatureCAService.getUserAndTraderByUserid(userid);
        System.out.println("user: "+user);
        String userId = user.get("USERID").toString();
        String qiyid = user.get("TRADERID").toString();
        if (ht != null && !ht.isEmpty()) {
            String chumrssdwbm = ht.get("CHUMDWBM").toString();
            String maisrssdwbm = ht.get("MAISDWBM").toString();
            String orgname = "";
            String orggz = "";
            String orghtz = "";
            String signtag = "";
            Map<String,Object> qyxx = signatureCAService.getTraderInfoById(qiyid);
            if (qiyid.equals(maisrssdwbm)) {
                signtag = "MSR";
                // 是否盖章
                if (!this.signatureCAService.getQzZT(hetid, "1")) {
                    // 插入买受人签名数据
                    if (this.signatureCAService.getSign(hetid, signtag)) {
                        ret.put("caSign", "1");
                    }
                    else {
                        String abc = this.signatureCAService.insertSign(request,
                                objtag, signtag, signclob, "2", ttgcasign);
                        ret.put("caSign", abc);
                    }
                    if (this.signatureCAService.searchSignClob(signclob)) {
                        orgname = ht.get("maisdwmc").toString();
                        if (this.signatureCAService.saveCustomQz(hetid, "1",
                                orgname)) {
                            ret.put("result", "2");
                        }
                        else {
                            ret.put("result", "0");
                        }
                    }
                    else {
                        ret.put("result", "0");
                    }
                }
                else {
                    ret.put("result", "4");
                }
            }
            else {
                String chumrtype = "0";
                System.out.println("ht: "+ht);
                String chumrorgname = ht.get("CHUMDWMC").toString();
                signtag = "CMR";
                if (qyxx != null) {
                    chumrtype = "3";
                    signtag = "CMRJS";
                    System.out.println("qyxx: "+qyxx);
                    chumrorgname = qyxx.get("FULLNAME").toString();
                }

                if (!this.signatureCAService.getQzZT(hetid, signtag)) {
                    // 插入出卖人数据
                    if (this.signatureCAService.getSign(hetid, signtag)) {
                        ret.put("caSign", "1");
                    }
                    else {
                        String abc = this.signatureCAService.insertSign(request,
                                objtag, signtag, signclob, "2", ttgcasign);
                        ret.put("caSign", abc);
                    }
                    if (this.signatureCAService.searchSignClob(signclob)) {
                        // 判断有无公章或者合同章
                        if (this.signatureCAService.saveCustomQz(hetid, chumrtype, chumrorgname)) {
                            ret.put("result", "1");
                        } else {
                            ret.put("result", "0");
                        }
                    } else {
                        ret.put("result", "0");
                    }
                } else {
                    ret.put("result", "1");
                }

            }
        }
        else {
            ret.put("result", "0");
        }
        return JsonUtil.mapToJson(ret);
    }



    /**
     * 这是CA调用印章显示的方法，用途是获取加密串返回后和原文验签，以便显示印章
     *
     * @param json   {"hetid":"5051","hettype":"TL"}
     * @return
     */
    @PostMapping(value = "/searchSignCA.jspx")
    @ResponseBody
//    public String searchSignCA(HttpServletRequest request,
//                        @RequestParam("hetid") String hetid,
//                        @RequestParam("hettype") String hettype) {
    public String searchSignCA(@RequestBody()String json) {
        System.out.println("json: "+json);

        JsonParser jsonParser = new JsonParser();
        JsonElement jsonElement = jsonParser.parse(json);
        JsonObject jsonObject = jsonElement.getAsJsonObject();

        String hetid = jsonObject.get("hetid").getAsString();
        String hettype = jsonObject.get("hettype").getAsString();


        Map<String , Object> ht = new HashMap();
        Map ret = new HashMap();
        if (hettype.equals("GL")) {
            ht = this.signatureCAService.getGlHeInfoById(hetid);
        }
        else if (hettype.equals("TL")) {
            ht = this.signatureCAService.getHeInfoById(hetid);
        }
        else if (hettype.equals("SL")) {
            ht = this.signatureCAService.getHeSLInfoById(hetid);
        }

        if (null != ht && !ht.isEmpty()) {
            List ttgresult = new ArrayList();

            // 查chumdwbm
            List ttgresult3 = this.signatureCAService.searchca(hetid, "CMRJS", "3");
            if (ttgresult3 != null && ttgresult3.size() > 0) {
                ttgresult.add(ttgresult3);
            }
            // 查maisdwbm
            List ttgresult1 = this.signatureCAService.searchca(hetid, "MSR", "1");
            if (ttgresult1 != null && ttgresult1.size() > 0) {
                ttgresult.add(ttgresult1);
            }

            // 查maisdwbm
            List ttgresult0 = this.signatureCAService.searchca(hetid, "CMR", "0");
            if (ttgresult0 != null && ttgresult0.size() > 0) {
                ttgresult.add(ttgresult0);
            }

            ret.put("result", ttgresult);
        }
        else {
            ret.put("result", "0");
        }

        return JsonUtil.mapToJson(ret);
    }
}
